package com.xmwzw.catpu.item;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.mikepenz.fastadapter.items.AbstractItem;
import com.xmwzw.catpu.R;
import com.xmwzw.catpu.SoftDetailActivity;
import com.xmwzw.catpu.bean.SoftBean;
import com.xmwzw.catpu.databinding.ItemSoftwareBinding;
import com.xmwzw.catpu.util.Constant;

import java.util.List;

/**
 * @author : wangzw
 * @date : 20-3-31上午11:36
 * @desc :
 */
public class SoftItem extends AbstractItem<SoftItem, SoftItem.ViewHolder> {
    private SoftBean softBean;
    private boolean isResult;

    public SoftItem fillData(boolean isResult, SoftBean softBean) {
        this.softBean = softBean;
        this.isResult = isResult;
        return this;
    }

    public SoftBean getSoftBean() {
        return softBean;
    }

    @Override
    public ViewHolder getViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public int getType() {
        return SoftItem.class.hashCode();
    }

    @Override
    public void bindView(final ViewHolder holder, List<Object> payloads) {
        super.bindView(holder, payloads);
        holder.itemSoftwareBinding.tvNo.setText(softBean.no);
        holder.itemSoftwareBinding.tvName.setText(softBean.name);
        holder.itemSoftwareBinding.tvVersion.setText("当前版本:" + softBean.version);
        holder.itemSoftwareBinding.tvDownload.setText(softBean.downloadNo);

        Glide.with(holder.itemSoftwareBinding.icLogo.getContext()).load(softBean.icon_url)
                .into(holder.itemSoftwareBinding.icLogo);

        holder.itemSoftwareBinding.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(holder.itemSoftwareBinding.tvDetail.getContext(), SoftDetailActivity.class);
                intent.putExtra(Constant.KEY_SOFT_BEAN, softBean);
                holder.itemSoftwareBinding.tvDetail.getContext().startActivity(intent);
            }
        });

        holder.itemSoftwareBinding.tvDetail.setVisibility(isResult ? View.GONE : View.VISIBLE);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_software;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ItemSoftwareBinding itemSoftwareBinding;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemSoftwareBinding = DataBindingUtil.bind(itemView);

        }
    }
}
